﻿// See https://aka.ms/new-console-template for more information
using ConsoleApp1;
using FuzzySharp;
using JiebaNet.Segmenter;
using JiebaNet.Segmenter.Common;

var segmenter = new JiebaSegmenter();
segmenter.LoadUserDict("userdict.txt");
var segments = segmenter.Cut("我来到北京清华大学", cutAll: true);
Console.WriteLine("【全模式】：{0}", string.Join("/ ", segments));

segments = segmenter.Cut("我来到北京清华大学");  // 默认为精确模式
Console.WriteLine("【精确模式】：{0}", string.Join("/ ", segments));

segments = segmenter.Cut("他来到了网易杭研大厦");  // 默认为精确模式，同时也使用HMM模型
Console.WriteLine("【新词识别】：{0}", string.Join("/ ", segments));

segments = segmenter.CutForSearch("小明硕士毕业于中国科学院计算所，后在日本京都大学深造"); // 搜索引擎模式
Console.WriteLine("【搜索引擎模式】：{0}", string.Join("/ ", segments));

segments = segmenter.Cut("结过婚的和尚未结过婚的");
Console.WriteLine("【歧义消除】：{0}", string.Join("/ ", segments));

segments = segmenter.Cut("linezerodemo机器学习学习机器");
Console.WriteLine("【用户字典】：{0}", string.Join("/ ", segments));

//词频统计
var s = "此领域探讨如何处理及运用自然语言。自然语言生成系统把计算机数据转化为自然语言。自然语言理解系统把自然语言转化为计算机程序更易于处理的形式。";
var freqs = new Counter<string>(segmenter.Cut(s));
foreach (var pair in freqs.MostCommon(5))
{
    Console.WriteLine($"{pair.Key}: {pair.Value}");
}

//new TestDemo().CutDemo();

//Console.ReadKey();

// 使用示例
var similarity = new ChineseTextSimilarity();
/*var text1 = "我喜欢编程和人工智能";
var text2 = "我热爱编程与机器学习";*/
var text1 = "销售运营经理/主管";
var text2 = "销售经理/销售主管";

Console.WriteLine("");
Console.WriteLine(text1);
Console.WriteLine("【精确模式】：{0}", string.Join(" ", segmenter.Cut(text1)));
Console.WriteLine("【全模式】  ：{0}", string.Join(" ", segmenter.Cut(text1, cutAll: true)));


Console.WriteLine("");
Console.WriteLine(text2);
Console.WriteLine("【精确模式】：{0}", string.Join(" ", segmenter.Cut(text2)));
Console.WriteLine("【全模式】  ：{0}", string.Join(" ", segmenter.Cut(text2, cutAll: true)));


var jaccard = similarity.CalculateJaccardSimilarity(text1, text2);
var cosine = similarity.CalculateCosineSimilarity(text1, text2);

Console.WriteLine("");
Console.WriteLine($"Jaccard相似度: {jaccard:P}");
Console.WriteLine($"余弦相似度   : {cosine:P}");

Console.WriteLine("");
Console.WriteLine("分词前相似度--");
Console.WriteLine($"简单比例匹配 Ratio: {FuzzyMatcher.Ratio(text1, text2)}%");
Console.WriteLine($"最佳子串匹配 PartialRatio: {FuzzyMatcher.PartialRatio(text1, text2)}%");
Console.WriteLine($"词序无关匹配 TokenSortRatio: {FuzzyMatcher.TokenSortRatio(text1, text2)}%");
Console.WriteLine($"集合相似度   TokenSetRatio: {FuzzyMatcher.TokenSetRatio(text1, text2)}%");

//预处理中文，进行分词，去掉单字
//分词模式为 “精确模式”
var isCutAll = false;
var cut1 = FuzzyMatcher.PreprocessChinese(text1, isCutAll);
var cut2 = FuzzyMatcher.PreprocessChinese(text2, isCutAll);
Console.WriteLine("");
Console.WriteLine("分词结果(精准模式)--");
Console.WriteLine(cut1);
Console.WriteLine(cut2);

Console.WriteLine("");
Console.WriteLine("精准模式分词后相似度--");
Console.WriteLine($"简单比例匹配 Ratio: {FuzzyMatcher.Ratio(cut1, cut2)}%");
Console.WriteLine($"最佳子串匹配 PartialRatio: {FuzzyMatcher.PartialRatio(cut1, cut2)}%");
Console.WriteLine($"词序无关匹配 TokenSortRatio: {FuzzyMatcher.TokenSortRatio(cut1, cut2)}%");
Console.WriteLine($"集合相似度   TokenSetRatio: {FuzzyMatcher.TokenSetRatio(cut1, cut2)}%");

//分词模式为 “全模式”
isCutAll = true;
cut1 = FuzzyMatcher.PreprocessChinese(text1, isCutAll);
cut2 = FuzzyMatcher.PreprocessChinese(text2, isCutAll);
Console.WriteLine("");
Console.WriteLine("分词结果(全模式)--");
Console.WriteLine(cut1);
Console.WriteLine(cut2);

Console.WriteLine("");
Console.WriteLine("全模式分词后相似度--");
Console.WriteLine($"简单比例匹配 Ratio: {FuzzyMatcher.Ratio(cut1, cut2)}%");
Console.WriteLine($"最佳子串匹配 PartialRatio: {FuzzyMatcher.PartialRatio(cut1, cut2)}%");
Console.WriteLine($"词序无关匹配 TokenSortRatio: {FuzzyMatcher.TokenSortRatio(cut1, cut2)}%");
Console.WriteLine($"集合相似度   TokenSetRatio: {FuzzyMatcher.TokenSetRatio(cut1, cut2)}%");

Console.ReadKey();
